﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Mapeo;
using BE;
using NHibernate;
using NHibernate.Criterion;
using System.Data;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class ConceptoTesoreria_DAL:Controlador
    {
        public void Insertar(ConceptoTesoreria tipocp)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        tipocp.ID = "0";
                        Sesion.Save(tipocp);
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }

        public void Actualizar(ConceptoTesoreria tipocp)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        Sesion.Update(tipocp);
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }

        public void Eliminar(String id)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        ConceptoTesoreria tipocp = (ConceptoTesoreria)AyudaNHibernate.ObtenerEntidadPorID("ConceptoTesoreria", id);
                        Sesion.Delete(tipocp);
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }
        
        public DataTable listado(String Codigo, String Nombre)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    MySqlCommand SqlCmd = new MySqlCommand();
                    SqlCmd.Connection = (MySqlConnection)Sesion.Connection;
                    Trans.Enlist(SqlCmd);
                    SqlCmd.CommandText = "sp_ListadoConceptoTesoreria";
                    SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlCmd.Parameters.AddWithValue("@in_codigo", Codigo);
                    SqlCmd.Parameters.AddWithValue("@in_nombre", Nombre);


                    SqlCmd.ExecuteNonQuery();
                    MySqlDataAdapter da = new MySqlDataAdapter(SqlCmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    Trans.Commit();
                    return dt;
                }
            }
        }

        public ConceptoTesoreria obtenerConceptoPorId(String ID)
        {
            try
            {
                return (ConceptoTesoreria)AyudaNHibernate.ObtenerEntidadPorID("ConceptoTesoreria", ID);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}
